<template>
	<view class="ljapp">
		<view class="content">
			<view class="item" v-for="(item,index) in group.list" :key="index">
				<view class="file">
					<view class="u-flex u-flex-y-center" @click="openFile(item)">
						<image :src="'/static/lj/common/filepdf.png'" mode=aspectFill></image>
						<view>
							<view class="tit">{{item.report_filename}}</view>
							<view class="ftime">{{item.report_time}}</view>
						</view>
					</view>
				</view>
				<view class="con">
					诊断结果：{{item.diagnose}}
				</view>
				<view class="foot u-flex u-flex-between">
					<view>诊断医生：{{item.doctor_info&&item.doctor_info.name?item.doctor_info.name:''}}</view>
					<view>诊断日期：{{$u.timeFormat(item.dptime)}}</view>
				</view>
			</view>
			
			<lj-empty v-if="group.loaded&&group.list.length<=0">暂无报告</lj-empty>
			<view v-else>
				<u-loadmore
					:status="loading?'loading':(group.loaded?'nomore':'loadmore')"
					fontSize="28rpx"
					color="#d9d9d9"
					nomoreText="已加载全部"
					marginTop="30rpx"
					marginBottom="0"
					@loadmore="getList"
				></u-loadmore>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				group:{
					page: 1,
					limit: 10,
					list: [],
					loaded: false,
				},
				loading: false,
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init(){
				this.group.page = 1
				this.group.list = []
				this.group.loaded = false
				this.loading = false
			
				this.getList();
			},
			getList(){
				var that = this, group = that.group;
				if(that.loading || group.loaded) return;
			
				that.loading = true
			
				this.$Http({
					url: 'getMyReportList',
					data: {
						page: group.page,
						limit: group.limit,
					},
					type:'POST',
				}).then(res=>{
					if(res.code == 1){
						var list = res.data;
						group.loaded = list.length < group.limit;
						group.page ++;
						group.list = group.list.concat(list);
					}
					
					that.loading = false
				}).catch(()=>{
					that.loading = false
				});
			},
			onReachBottom(){
				this.getList()
			},
			
			// 判断文件类型
			whatFileType(url){
				let sr = url.lastIndexOf('.') //  最后一次出现的位置
				let fileType = url.substr((sr+1)) // 截取url的类型
				return(fileType)
			},
			openFile(item){
				console.log('openFile',item)
				var that = this
				item.report_filetype = 'pdf'
				if(item.report_filetype=='pdf'){
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					
					let fileType = this.whatFileType(item.report_url)
					console.log(wx.env.USER_DATA_PATH + "/"+ (new Date().getTime()) + "."+fileType)
					uni.downloadFile({
						url: that.$base.img(item.report_url),
						// filePath: wx.env.USER_DATA_PATH + "/"+ (new Date().getTime()) + "."+fileType,
						success(res){
							uni.hideLoading()
							var filePath = res.tempFilePath
							uni.showLoading({
								title: '正在打开',
								mask: true
							})
							
							uni.openDocument({
								filePath: filePath,
								// fileType: item.type,
								// showMenu: true,
								// flieType: fileType,
								success(res) {
									uni.hideLoading()
									
								},
								fail(err){
									console.log('openDocument', err)
									uni.hideLoading()
									that.$u.toast('打开失败');
								}
							})
						},
						fail(err){
							console.log('downloadFile', err)
							uni.hideLoading()
							that.$u.toast('加载失败');
						}
					})
				}else{
					that.$u.toast('开始下载【'+item.report_filename+'】');
					uni.downloadFile({
						url: that.$base.img(item.report_url),
						success: (res) => {
							if (res.statusCode === 200) {
								console.log('下载成功');
								that.$u.toast('下载成功');
							}
						}
					})
				}
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesL/style.less';
	
	.content{
		padding: 23rpx 20rpx;
	}
	
	.item{
		background-color: #fff;
		padding: 20rpx;
		border-radius: 8rpx;
		margin-bottom: 20rpx;
		
		.file{
			background: #f6f6f6;
			border-radius: 10rpx;
			padding: 24rpx;
			
			image{
				display: block;
				width: 66rpx;
				min-width: 66rpx;
				height: 85rpx;
				margin-left: 30rpx;
				margin-right: 40rpx;
			}
			.tit{
				font-size: 32rpx;
				color: #666666;
			}
			.ftime{
				font-size: 26rpx;
				margin-top: 8rpx;
				color: #666666;
			}
		}
		
		.con{
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #656565;
		}
		.foot{
			margin: 20rpx -20rpx 0;
			padding: 20rpx 20rpx 0;
			border-top: 2rpx solid #E9E9E9;
			font-size: 24rpx;
			color: #656565;
		}
	}
</style>
